package com.vincent.filepicker.ability;

import com.vincent.filepicker.FolderListHelper;
import com.vincent.filepicker.PermissionBridge;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.security.SystemPermission;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import static com.vincent.filepicker.PermissionBridge.EVENT_PERMISSION_DENIED;
import static com.vincent.filepicker.PermissionBridge.EVENT_PERMISSION_GRANTED;
import static ohos.bundle.IBundleManager.PERMISSION_GRANTED;


/**
 * Created by Vincent Woo
 * Date: 2016/10/12
 * Time: 16:21
 */

public abstract class BaseAbility extends Ability implements PermissionBridge.OnPermissionStateListener{
    private static final int RC_READ_EXTERNAL_STORAGE = 123;
    private static final String TAG = BaseAbility.class.getName();

    protected FolderListHelper mFolderHelper;
    protected boolean isNeedFolderList;
    public static final String IS_NEED_FOLDER_LIST = "isNeedFolderList";
    public static final int RESULT_OK = -1;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        new PermissionBridge().setOnPermissionStateListener(this);
        isNeedFolderList = getIntent().getBooleanParam(IS_NEED_FOLDER_LIST, false);
        if (isNeedFolderList) {
            mFolderHelper = new FolderListHelper();
            mFolderHelper.initFolderListView(this);
        }
    }

    @Override
    protected void onPostActive() {
        super.onPostActive();
        readExternalStorage();
    }

    /**
     * Read external storage file
     */
    private void readExternalStorage() {
        List<String> permissions =
                new LinkedList<String>(
                        Arrays.asList(
                                SystemPermission.WRITE_USER_STORAGE,
                                SystemPermission.READ_USER_STORAGE,
                                SystemPermission.MICROPHONE,
                                SystemPermission.CAMERA));
        permissions.removeIf(
                permission ->
                        verifySelfPermission(permission) == PERMISSION_GRANTED || !canRequestPermission(permission));

        if (!permissions.isEmpty()) {
            requestPermissionsFromUser(permissions.toArray(new String[permissions.size()]), RC_READ_EXTERNAL_STORAGE);
        } else {
            PermissionBridge.getHandler().sendEvent(EVENT_PERMISSION_GRANTED);
        }

    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        if (requestCode != RC_READ_EXTERNAL_STORAGE) {
            return;
        }
        for (int grantResult : grantResults) {
            if (grantResult != PERMISSION_GRANTED) {
                PermissionBridge.getHandler().sendEvent(EVENT_PERMISSION_DENIED);
                terminateAbility();
                return;
            }
        }
    }

    public void onBackClick(Component view) {
        terminateAbility();
    }
}
